package com.tuo51.evaluate.repositories.evaluatev2.impl;

import com.matt.commons.exception.BusinessException;
import com.matt.foundation.model.ErrorCode;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.tuo51.evaluate.repositories.evaluatev2.EvaluateNativeRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

//import com.matt.repository.base.BaseNativeSqlRepository;

@Repository
public class EvaluateNativeRepositoryImple extends BaseNativeSqlRepository implements EvaluateNativeRepository {


    @Override
    public Long fullEvaluateDurationForRecord(String recordId){
        String sql = "SELECT TIME_TO_SEC( " +
                "timediff((SELECT MAX(a.`created`) FROM `tb_evaluate_record_answer` a WHERE a.`recordId` = :1 )," +
                "         (SELECT MIN(a.`created`) FROM `tb_evaluate_record_answer` a WHERE  a.`recordId` = :1 )) )";

        if(StringUtils.isEmpty(recordId)){
            throw new BusinessException(ErrorCode.OBJECT_ID_NULL, "不能为空");
        }

        List param = Stream.of(recordId).collect(Collectors.toList());
        sql = formatSql(sql, param);

        Long time = sqlSingleResult(sql);
        return time;
    }

}
